package com.lxx.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lxx.entity.User;
import com.lxx.service.impl.RedisService;

@Controller
@RequestMapping("/redis")
public class RedisController {

	@Autowired
	private RedisService redisService;
	
	/**
	 * redis String test
	 * @return
	 */
	@RequestMapping("/string")
	@ResponseBody
	public String getString(){
		String object = (String) redisService.get("who_am_i");
		if(StringUtils.isNotBlank(object)){
			System.out.println("String has redis");
		}else{
			System.out.println("String hasnot redis");
			redisService.set("who_am_i", "i am redis");
		}
		return "String is OK";
	}
	
	/**
	 * hash test
	 * @param key 
	 * @param hashKey 
	 * @return
	 */
	@RequestMapping("/hash")
	@ResponseBody
	public String getHashSet(String key, String hashKey, long expire){
		User user = (User) redisService.hmGet(key, hashKey);
		if(null != user){
			System.out.println("hash has redis");
			System.out.println(user.toString());
		}else{
			user = new User();
			user.setId("9527");
			user.setLoginName("好像一条狗");
			redisService.hmSet(key, hashKey, user, expire);
		}
		return "Hash is OK";
	}
}
